//
//  SignInputViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/9.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "SignInputViewController.h"
#import "SignViewController.h"
#import "BaseNavigationController.h"
@interface SignInputViewController ()

@end

@implementation SignInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view from its nib.
    [self setupNavigation];
}
-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SVProgressHUDDidReceiveTouchEventNotification
                                               object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDDidReceiveTouchEventNotification object:nil];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
#pragma mark 点击取消等待界面
- (void)handleNotification:(NSNotification *)notif
{
    [SVProgressHUD dismiss];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
-(void)setupNavigation
{
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];;
    [nextBtn setTitle:@"下一步" forState:0];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:0];
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:64];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:nextBtn];
    
    self.title = @"输入机构号码";
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [backBtn setTitle:@"返回" forState:0];
    [backBtn setTitleColor:[UIColor whiteColor] forState:0];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:64];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
}
-(void)backAction:(UIButton *)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void)nextAction:(UIButton *)sender
{
    
    [self.groupField resignFirstResponder];
    if (self.isPushSignVC) {
        if (![self.groupField.text isEqualToString:@""]) {
            [SVProgressHUD showWithStatus:@"加载中.." maskType:SVProgressHUDMaskTypeBlack];
            NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"VoluteerGroup",@"method":@"loadVoluteerGroupBycode",@"requestId":[NSNull new],@"requestParam":@[self.groupField.text]};
            
            [Utils requestDic:parameters andRusult:^(id result) {
                if ([result isKindOfClass:[NSDictionary class]]) {
                    
                    if(![result[@"responseData"] isKindOfClass:[NSNull class]])
                    {
                        [SVProgressHUD dismiss];
                        NSDictionary *responseData = result[@"responseData"];
                        if (responseData&&responseData.allKeys.count>0) {
                            NSString *groupName = responseData[@"groupName"];
                            NSString *groupId = responseData[@"groupId"];
                            [self pushSignViewController:groupName andGroupId:groupId];
                        }
                    }else
                    {
                        [SVProgressHUD showErrorWithStatus:@"没有该机构" maskType:SVProgressHUDMaskTypeBlack];
                    }
                    
                    
                }
                
            } andError:^(id result) {
                [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
                
                
            }];
        }else
        {
            [Utils showAlert:@"请输入机构号码"];
        }
    }else
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    
    
}
#pragma mark 推到注册界面
-(void)pushSignViewController:(NSString *)name andGroupId:(NSString *)groupId
{
//    SignViewController *signVC = [[SignViewController alloc]initWithNibName:@"SignViewController" bundle:nil];
    SignViewController *signVC = [[SignViewController alloc]initWithNibName:@"SignViewController" bundle:nil];
    [signVC setZbarId:groupId];
    [signVC setZbarStr:name];
    [self.navigationController pushViewController:signVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
